package software.hust.com.hust_last.util;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.List;


public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerAdapter<T>.RecyclerHolder> {

    @Override
    public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerHolder(getItem(viewType));
    }

    @Override
    public void onBindViewHolder(RecyclerHolder holder, int position) {
        if (getData() == null || position < 0 || position >= getData().size()) {
            return;
        }
        holder.bindData(position, getData().get(position));
    }

    @Override
    public int getItemCount() {
        return getData() == null ? 0 : getData().size();
    }

    protected abstract List<T> getData();

    protected abstract RecyclerItem<T> getItem(int viewType);

    public class RecyclerHolder extends RecyclerView.ViewHolder {
        RecyclerItem<T> item;

        public RecyclerHolder(RecyclerItem<T> item) {
            super(item.getView());
            this.item = item;
        }

        public void bindData(int position, T data) {
            item.bindData(position, data);
        }
    }
}
